package com.bookshop.ui.component;

import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.JTextComponent;

import com.bookshop.ui.ITabelCallBack;

public class Tabel extends JTable implements TableModelListener {

	private final int[] mUneditabelColumns;
	private final MyTableModel mModel;
	private final ITabelCallBack mCallBack;
	public Tabel(ITabelCallBack pCallBack, Object[][] rowData, String[] columnNames, int[] pUneditabelColumns) {
		mCallBack = pCallBack;
		mUneditabelColumns = pUneditabelColumns;
		mModel = new MyTableModel(rowData, columnNames);
		setModel(mModel);
		getTableHeader().setEnabled(false);
		setOpaque(false);
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		TableCellEditor tce = this.getDefaultEditor(String.class);
		if (tce instanceof DefaultCellEditor) {
			DefaultCellEditor dce = (DefaultCellEditor) tce;
			final Component c = dce.getComponent();
			c.addFocusListener(new FocusListener() {
				@Override
				public void focusGained(FocusEvent arg0) {
					if (c instanceof JTextComponent) {
						((JTextComponent) c).selectAll();
					}
				}

				@Override
				public void focusLost(FocusEvent arg0) {
				}
			});
		}
	}
	
	public void updateData(Object[][] data) {
		mModel.rowData = data;
		getSelectionModel().clearSelection();
		this.repaint();
		this.updateUI();
	}
	
	@Override
	public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
		Component c = super.prepareRenderer(renderer, row, column);
		if (c instanceof JComponent) {
			JComponent jc = (JComponent) c;
			jc.setToolTipText(getValueAt(row, column).toString());
		}
		return c;
	}
	
	@Override
	public void tableChanged(TableModelEvent e) {
		super.tableChanged(e);
		if (mModel == null || getSelectedRow() == -1 || mCallBack == null) return;
		
		mCallBack.callback(mModel.getRow(getSelectedRow()));
	}
	
	public Object getSelectedContent(int col){
		return ((MyTableModel) getModel()).getValueAt(getSelectedRow(), col);
	}
	
	class MyTableModel extends AbstractTableModel{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		Object[][] rowData;
		String[] columnNames;
		public MyTableModel(Object[][] rowData, String[] columnNames) {
			this.rowData = rowData;
			this.columnNames = columnNames;
		}
		
		public Object[] getRow(int pIndex) {
			int colCount = getColumnCount();
			Object[] result = new Object[colCount];
			for (int i = 0; i < colCount; i++) {
				result[i] = getValueAt(pIndex, i);
			}
			return result;
		}
		
		public void setRow(Object[][] rowData) {
			this.rowData = rowData;
		}
		
		@Override
		public Class<? extends Object> getColumnClass(int columnIndex){
			return getValueAt(0, columnIndex).getClass();
		}
		
		@Override
		public int getColumnCount() {
			return columnNames.length;
		}

		@Override
		public int getRowCount() {
			return rowData.length;
		}

		@Override
		public Object getValueAt(int row, int col) {
			return rowData[row][col];
		}
		
		@Override
		public boolean isCellEditable(int row, int col) {
			for (int i = 0; i < mUneditabelColumns.length; i++) {
				if (col == mUneditabelColumns[i]) return false;
			}
			return true;
		}
		@Override
		public void setValueAt(Object value, int row, int col) {
			rowData[row][col] = value;
			fireTableCellUpdated(row, col);
		}
		@Override
		public String getColumnName(int col) {
			return columnNames[col];
		}
	}


}


